package com.pro.control;

import com.pro.entity.*;
import com.pro.service.MicrocircleService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/microcircle")
public class MicrocircleController {

    @Autowired
    private MicrocircleService microcircleService;

    @Autowired
    private FastDFSClientUtil fsc;

    /**
     * @ClassName getMicrocircleList
     * @Author 谭金诚
     * @Date 2021/11/24
     */
    @RequestMapping("/getJsActicleCategroy")
    @ResponseBody
    public JsonData getJsActicleCategroy(Integer acticleCategroyId){
        List<JsActicleCategroy> jsActicleCategroy = microcircleService.getJsActicleCategroy(acticleCategroyId);
        return jsActicleCategroy != null ? JsonData.buildSuccess(jsActicleCategroy) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getMicrocircleList
     * @Author 谭金诚
     * @Date 2021/11/24
     */
    @RequestMapping("/getMicrocircleList")
    @ResponseBody
    public JsonData getMicrocircleList(JsMicrocircle jsMicrocircle){
        List<JsMicrocircle> microcircleList = microcircleService.getMicrocircleList(jsMicrocircle);
        return microcircleList != null ? JsonData.buildSuccess(microcircleList) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getCollectionNum
     * @Author 谭金诚
     * @Date 2021/11/24
     */
    @RequestMapping("/getCollectionNum")
    @ResponseBody
    public JsonData getCollectionNum(Integer microcircleId){
        Integer collectionNum = microcircleService.getCollectionNum(microcircleId);
        return collectionNum != 0 ? JsonData.buildSuccess(collectionNum) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delMicroCircle
     * @Author 谭金诚
     * @Date 2021/11/24
     */
    @RequestMapping("/delMicroCircle")
    @ResponseBody
    public JsonData delMicroCircle(Integer microcircleId){
        boolean b = microcircleService.delMicroCircle(microcircleId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addZtree
     * @Author 谭金诚
     * @Date 2021/11/25
     */
    @RequestMapping("/addZtree")
    @ResponseBody
    public JsonData addZtree(JsActicleCategroy jsActicleCategroy){
        boolean b = microcircleService.addZtree(jsActicleCategroy);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delZtree
     * @Author 谭金诚
     * @Date 2021/11/25
     */
    @RequestMapping("/delZtree")
    @ResponseBody
    public JsonData delZtree(Integer acticleCategroyId){
        boolean b = microcircleService.delZtree(acticleCategroyId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName editZtree
     * @Author 谭金诚
     * @Date 2021/11/25
     */
    @RequestMapping("/editZtree")
    @ResponseBody
    public JsonData editZtree(String artcleCategroyName, Integer acticleCategroyId){
        boolean b = microcircleService.editZtree(artcleCategroyName, acticleCategroyId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addLog
     * @Author 谭金诚
     * @Date 2021/11/25
     */
    @RequestMapping("/addLog")
    @ResponseBody
    public JsonData addLog(JsLog jsLog){
        boolean b = microcircleService.addLog(jsLog);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName selLog
     * @Author 谭金诚
     * @Date 2021/11/25
     */
    @RequestMapping("/selLog")
    @ResponseBody
    public JsonData selLog(JsLog jsLog){
        List<JsLog> jsLogs = microcircleService.selLog(jsLog);
        return jsLogs != null ? JsonData.buildSuccess(jsLogs) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delLog
     * @Author 谭金诚
     * @Date 2021/11/25
     */
    @RequestMapping("/delLog")
    @ResponseBody
    public JsonData delLog(Integer logId){
        boolean b = microcircleService.delLog(logId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getDraftMicrocircle
     * @Author 谭金诚
     * @Date 2021/11/28
     */
    @RequestMapping("/getDraftMicrocircle")
    @ResponseBody
    public JsonData getDraftMicrocircle(Integer details){
        JsMicrocircle draftMicrocircle = microcircleService.getDraftMicrocircle(details);
        return draftMicrocircle != null ? JsonData.buildSuccess(draftMicrocircle) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName upDraftMicrocircle
     * @Author 谭金诚
     * @Date 2021/11/28
     */
    @RequestMapping("/upDraftMicrocircle")
    @ResponseBody
    public JsonData upDraftMicrocircle(JsMicrocircle jsMicrocircle){
        boolean b = microcircleService.upDraftMicrocircle(jsMicrocircle);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addDraftMirList
     * @Author 谭金诚
     * @Date 2021/11/29
     */
    @RequestMapping("/addDraftMirList")
    @ResponseBody
    public JsonData addDraftMirList(JsMicrocircle jsMicrocircle){
        jsMicrocircle.setArtcleKeywords("微圈");
        jsMicrocircle.setCreateDate(new Date());
        jsMicrocircle.setUpdateDate(new Date());
        jsMicrocircle.setReleaseDate(null);
        boolean b = microcircleService.addDraftMirList(jsMicrocircle);
        System.err.println(jsMicrocircle);
        return b != false ? JsonData.buildSuccess(jsMicrocircle) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addMirImg
     * @Author 谭金诚
     * @Date 2021/11/29
     */
    @RequestMapping("/addMirImg")
    @ResponseBody
    public JsonData addMirImg(JsMicrocircleImg jsMicrocircleImg){
        boolean b = microcircleService.addMirImg(jsMicrocircleImg);
        return b != false ? JsonData.buildSuccess(jsMicrocircleImg) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addMirList
     * @Author 谭金诚
     * @Date 2021/11/29
     */
    @RequestMapping("/addMirList")
    @ResponseBody
    public JsonData addMirList(JsMicrocircle jsMicrocircle){
        boolean b = microcircleService.addMirList(jsMicrocircle);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName queryMicroCircleClassification
     * @Author 谭金诚
     * @Date 2021/12/1
     */
    @RequestMapping("/queryMicroCircleClassification")
    @ResponseBody
    public JsonData queryMicroCircleClassification(Integer acticleCategroyId){
        JsActicleCategroy jsActicleCategroy = microcircleService.queryMicroCircleClassification(acticleCategroyId);
        return jsActicleCategroy != null ? JsonData.buildSuccess(jsActicleCategroy) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getMicroCircleComment
     * @Author 谭金诚
     * @Date 2021/12/1
     */
    @RequestMapping("/getMicroCircleComment")
    @ResponseBody
    public JsonData getMicroCircleComment(Integer commodityId){
        List<JsComment> microCircleComment = microcircleService.getMicroCircleComment(commodityId);
        return microCircleComment != null ? JsonData.buildSuccess(microCircleComment) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName hideComment
     * @Author 谭金诚
     * @Date 2021/12/1
     */
    @RequestMapping("/hideComment")
    @ResponseBody
    public JsonData hideComment(Integer id, Integer display){
        boolean b = microcircleService.hideComment(id,display);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delComment
     * @Author 谭金诚
     * @Date 2021/12/1
     */
    @RequestMapping("/delComment")
    @ResponseBody
    public JsonData delComment(Integer id){
        boolean b = microcircleService.delComment(id);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addMicroCircleComment
     * @Author 谭金诚
     * @Date 2021/12/3
     */
    @RequestMapping("/addMicroCircleComment")
    @ResponseBody
    public JsonData addMicroCircleComment(JsComment jsComment){
        boolean b = microcircleService.addMicroCircleComment(jsComment);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getStatistics
     * @Author 谭金诚
     * @Date 2021/12/7
     */
    @RequestMapping("/getStatistics")
    @ResponseBody
    public JsonData getStatistics(Integer acticleCategroyId){
        List<JsMicrocircle> hitsLikeNums = microcircleService.getStatistics();
        return hitsLikeNums != null ? JsonData.buildSuccess(hitsLikeNums) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName upload
     * @Author 谭金诚
     * @Date 2021/12/13
     */
    @PostMapping("/upload")
    @ResponseBody
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile[] file, HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String,Object>();
        String resAccessUrl = null;
        for (MultipartFile multipartFile : file) {
            try {
                //数据库文件路径
                String fileUrl = fsc.uploadFile(multipartFile);
                //前台传递路径
                request.setAttribute("msg", "成功上传文件，  '" + fileUrl + "'");
                resAccessUrl = fsc.getResAccessUrl(fileUrl);
                int i = resAccessUrl.lastIndexOf("/");
                String split = resAccessUrl.substring(i+1);
                map.put("src",resAccessUrl);
                map.put("title",split);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map != null ? JsonData.buildSuccess(map):JsonData.buildError("服务器错误");
    }

    /**
     * @ClassName delImgPreview
     * @Author 谭金诚
     * @Date 2021/12/14
     */
    @PostMapping("/delImgPreview")
    @ResponseBody
    public JsonData delImgPreview(Integer microcircleId){
        boolean b = microcircleService.delImgPreview(microcircleId);
        return b != false ? JsonData.buildSuccess(b) : JsonData.buildError("服务端错误");
    }


}
